package com.masterclient.app.businessmodule.homemodule.view.adapter.footer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.footertab.HomeFooterTabData;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;


public class HomePageXFootStickAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private HomeFooterTabData footerProducts;
    private XFootStickCallBack xFootStickCallBack;
    private String footerTabSwitch;

    public interface XFootStickCallBack{
        void tab_change(int index);
    }


    public HomePageXFootStickAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null,"",null);
    }

    public HomePageXFootStickAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, HomeFooterTabData footerProducts, String footerTabSwitch, XFootStickCallBack xFootStickCallBack) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.footerProducts = footerProducts;
        this.footerTabSwitch = footerTabSwitch;
        this.xFootStickCallBack = xFootStickCallBack;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    boolean hasDecoration = false;
    TabLayout tabLayout;
    ImageView iv;
    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        tabLayout = holder.itemView.findViewById(R.id.tabLayout);
        iv = holder.itemView.findViewById(R.id.iv_foot_stick);
        if (TextUtils.isEmpty(footerTabSwitch) || "0".equals(footerTabSwitch)){
//            iv.getLayoutParams().height = ScreenUtils.getScreenWidth()*footerProducts.getImage_height()/750;
            ViewTransformUtil.glideImageView(mContext,footerProducts.getImage(),iv,R.mipmap.defaults_1);
            iv.setVisibility(View.VISIBLE);
            tabLayout.setVisibility(View.GONE);

        }else {

            //热门推荐改为底部推荐
            if (footerProducts != null && footerProducts.getList().size() > 0) {
                init_cate_tablayout(tabLayout);
                tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        int position = tab.getPosition();
                        set_cate_tab(position, tabLayout);
                        if (xFootStickCallBack != null) {
                            xFootStickCallBack.tab_change(position);
                        }

                    }


                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
            }

            iv.setVisibility(View.GONE);
            tabLayout.setVisibility(View.VISIBLE);
        }
    }

    private int footerSelect;

    private void init_cate_tablayout(TabLayout tabLayout){
        if(footerProducts.getList().size() < 5){
            tabLayout.setTabMode (TabLayout.MODE_SCROLLABLE);
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        }else{
            tabLayout.setTabMode (TabLayout.MODE_FIXED);
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        }
        tabLayout.removeAllTabs();
        for (int i = 0; i < footerProducts.getList().size(); i++) {
            tabLayout.addTab(tabLayout.newTab());
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.homepage_tab_cate_item);
                View view = tab.getCustomView();
                TextView startTime = (TextView) view.findViewById(R.id.startTime);
                TextView status = (TextView) view.findViewById(R.id.status);
                startTime.setText(footerProducts.getList().get(i).getTitle());
                status.setText(footerProducts.getList().get(i).getSub_title());
            }
        }
        set_cate_tab(0,tabLayout);
    }

    private void set_cate_tab(int position,TabLayout tabLayout) {
        footerSelect = position;
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            View view = tabLayout.getTabAt(i).getCustomView();
            if (view == null) {
                return;
            }
            TextView startTime = (TextView) view.findViewById(R.id.startTime);
            TextView status = (TextView) view.findViewById(R.id.status);

            if (position == i) {
                startTime.setTextColor(Color.parseColor("#F23051"));
                status.setTextColor(Color.parseColor("#FFFFFF"));
                startTime.setTextSize(19);
                status.setTextSize(10);
                status.setBackgroundResource(R.drawable.homepage_cate_desc_bkg);
                startTime.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
            } else {
                startTime.setTextColor(Color.parseColor("#141922"));
                status.setTextColor(Color.parseColor("#999FAA"));
                startTime.setTextSize(19);
                status.setTextSize(10);
                status.setBackground(null);
                startTime.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));//加粗
            }
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 7;
    }

    public void set_tab_bkg(int type){
        if(tabLayout != null&&tabLayout.getVisibility() == View.VISIBLE){
            if(type == 0){
                tabLayout.setBackgroundColor(Color.parseColor("#F7F8FA"));
            }else{
                tabLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }
        }

        if(iv != null&&iv.getVisibility() == View.VISIBLE){
            if(type == 0){
                iv.setBackgroundColor(Color.parseColor("#F7F8FA"));
            }else{
                iv.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }
        }
    }
}
